package com.lifp.annotation.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Description 自定义日志注解@SysLog 切面类
 * @Author lfp
 * @Date 2022/8/24
 * @Version 1.0
 */
@Component
@Aspect
@Slf4j
public class AspectSysLog {

    //切点，标注了自定义的注解@SysLog的方法作为切点
    @Pointcut("@annotation(com.lifp.annotation.SysLog)")
    public void point(){}

    @Before("point()")
    public void before(){
        log.info("Before-》 前置通知");
    }

    @After("point()")
    public void after(){
        log.info("After-》 后置通知");
    }

    @AfterReturning("point()")
    public void afterReturning(){
        log.info("AfterReturning-》 返回通知");
    }

    @AfterThrowing("point()")
    public void afterThrowing(){
        log.info("AfterThrowing-》 异常通知");
    }

    @Around("point()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("Around-》 环绕通知");
        //创建ObjectMapper对象，用来对象转换
        ObjectMapper mapper = new ObjectMapper();

        //当前方法对象
        Signature signature = proceedingJoinPoint.getSignature();

        //类名
        String declaringTypeName = signature.getDeclaringTypeName();
        //方法名
        String methodName = signature.getName();

        //获取入参
        Object[] args = proceedingJoinPoint.getArgs();


        log.info("进入{}类的{}方法，参数为：{}",declaringTypeName,methodName,mapper.writeValueAsString(args));


        //必须让方法执行完成，否则会中断业务操作
        proceedingJoinPoint.proceed();
    }
}
